﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Doc;
using Spire.Doc.Documents;
namespace AHCS
{
   public class SpireDoc
    {
       public static void SpiresplitDoc(string fielsrc)
        {
            //实例化一个Document类，加载文档  
            Document original = new Document();
            original.LoadFromFile(fielsrc);
            //实例化Document类对象，并添加section  
            Document newWord = new Document();
            Section section = newWord.AddSection();
            //根据分页来拆分文档  
            int index = 0;
            //遍历文档所有section  
            foreach (Section sec in original.Sections)
            {
                //遍历文档所有子对象  
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph para = obj as Paragraph;
                        //复制并添加原有段落对象到新文档  
                        section.Body.ChildObjects.Add(para.Clone());
                        //遍历所有段落子对象  
                        foreach (DocumentObject parobj in para.ChildObjects)
                        {
                            if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak)
                            {
                                //获取段落分页并移除，保存新文档到文件夹  
                                int i = para.ChildObjects.IndexOf(parobj);
                                section.Body.LastParagraph.ChildObjects.RemoveAt(i);
                                newWord.SaveToFile(String.Format("results/out-{0}.docx", index), FileFormat.Docx);
                                index++;
                                //实例化Document类对象，添加section，将原文档段落的子对象复制到新文档  
                                newWord = new Document();
                                section = newWord.AddSection();
                                section.Body.ChildObjects.Add(para.Clone());
                                if (section.Paragraphs[0].ChildObjects.Count == 0)
                                {
                                    //移除第一个空白段落  
                                    section.Body.ChildObjects.RemoveAt(0);
                                }
                                else
                                {
                                    //删除分页符前的子对象  
                                    while (i >= 0)
                                    {
                                        section.Paragraphs[0].ChildObjects.RemoveAt(i);
                                        i--;
                                    }
                                }
                            }
                        }
                    }
                    //若对象为表格，则添加表格对象到新文档  
                    if (obj is Table)
                    {
                        section.Body.ChildObjects.Add(obj.Clone());
                    }
                }
            }
            //拆分后的新文档保存至指定文档  
            newWord.SaveToFile(String.Format("results/out-{0}.docx", index), FileFormat.Docx);
        }
    }
}
